package org.sport.presenter;

import net.sf.json.JSONObject;
import org.sport.logic.Logic_login;
import org.sport.model.jsonModel.Json_0_forget_passwd;
import org.sport.model.jsonModel.Json_0_set;
import org.sport.model.jsonModel.Json_0_sign_in;
import org.sport.model.jsonModel.Json_0_sign_up;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by zehong
 *
 * 主要用于对接登录功能，完成登录，注册，更新密码的需求的接口。
 *
 */

@Controller
public class Interface_login {


    @Autowired
    Logic_login logicForlogin;


    /**
     * 功能：注册
     * json参数：包含手机号码和密码
     * 返回：添加了注册结果
     */
    @RequestMapping(value = "/0/sign_up", method = RequestMethod.POST)
    public void userSignUp(@RequestBody Json_0_sign_up json_0_sign_up
            , HttpServletRequest request
            , HttpServletResponse response
    )
    {
        json_0_sign_up = logicForlogin.SignUp(json_0_sign_up);
        try {
            response.getWriter().print(JSONObject.fromObject(json_0_sign_up).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能：注册过后可以初步设置个人基本信息
     * json参数：包含手机号码和密码
     * 返回：添加了设置结果
     */
    @RequestMapping(value = "/0/set", method = RequestMethod.POST)
    public void userSet(@RequestBody Json_0_set json_0_set
            , HttpServletRequest request
            , HttpServletResponse response)
    {
        json_0_set=logicForlogin.Set(json_0_set);
        try {
            response.getWriter().print(JSONObject.fromObject(json_0_set).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能：登录
     * json参数：包含手机号码和密码
     * 返回：添加了登录结果
     */
    @RequestMapping(value = "/0/sign_in", method = RequestMethod.POST)
    public void userTest(@RequestBody Json_0_sign_in json_0_sign_in
            , HttpServletRequest request
            , HttpServletResponse response)
    {
        json_0_sign_in = logicForlogin.SignIn(json_0_sign_in);
        try {
            response.getWriter().print(JSONObject.fromObject(json_0_sign_in).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 功能：忘记密码，重新设置
     * json参数：包含手机号码和要改的新的密码
     * 返回：添加了更新密码的结果
     */
    @RequestMapping(value = "/0/forget_passwd", method = RequestMethod.POST)
    public void userTest(@RequestBody Json_0_forget_passwd json_0_forget_passwd
            , HttpServletRequest request
            , HttpServletResponse response)
    {
        json_0_forget_passwd = logicForlogin.ForgetPassward(json_0_forget_passwd);
        try {
            response.getWriter().print(JSONObject.fromObject(json_0_forget_passwd).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
